/**
 * 
 */
package info.niwota.web.apache;

import info.niwota.web.Debug;

import java.io.IOException;
import java.io.OutputStream;

import x.org.apache.http.Header;
import x.org.apache.http.HttpEntity;
import x.org.apache.http.HttpResponse;
import x.org.apache.http.HttpStatus;
import x.org.apache.http.StatusLine;
import x.org.apache.http.client.HttpClient;
import x.org.apache.http.client.methods.HttpPost;
import x.org.apache.http.entity.mime.MIME;
import x.org.apache.http.entity.mime.MultipartEntity;
import x.org.apache.http.entity.mime.content.AbstractContentBody;
import x.org.apache.http.entity.mime.content.StringBody;
import x.org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class FileUploader {
	public static class EntityContentBody extends AbstractContentBody {
		private HttpEntity entity = null;

		public EntityContentBody(HttpEntity entity, String mimeType) {
			super(mimeType);
			this.entity = entity;
		}

		private final String filename = "file";

		public void writeTo(final OutputStream out) throws IOException {
			entity.writeTo(out);
		}

		public String getTransferEncoding() {
			return MIME.ENC_BINARY;
		}

		public String getCharset() {
			return null;
		}

		public long getContentLength() {
			return -1;
		}

		public String getFilename() {
			return this.filename;
		}
	}

	private static final String TAG = "FileUploader";

	public FileUploader() {
	}
	
	private static String getMimeType(Header type) {
		return (type == null ? "application/octet-stream" : type.getValue());
	}

	public static boolean upload(String postback, HttpEntity entity, String peer, String uri,
			String name, int access, String cid, String tag) {
		if (Debug.DEBUG) {
			Log.d(TAG, "cid: " + cid + " tag: " + tag + " postback: "
					+ postback + " peer: " + peer + " uri: " + uri + " name: "
					+ name + " access: " + access);
		}
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(postback);

			MultipartEntity multipart = new MultipartEntity();

			long size = entity.getContentLength();
			String mimeType = getMimeType(entity.getContentType());
			
			multipart.addPart("cid", new StringBody(cid));
			multipart.addPart("tag", new StringBody(tag));
			multipart.addPart("peer", new StringBody(peer));
			multipart.addPart("uri", new StringBody(uri));
			multipart.addPart("name", new StringBody(name));
			multipart.addPart("size", new StringBody(size + ""));
			multipart.addPart("type", new StringBody(mimeType));
			multipart.addPart("access", new StringBody(access + ""));

			multipart.addPart("file", new EntityContentBody(entity, mimeType));

			httpPost.setEntity(multipart);

			HttpResponse response = httpClient.execute(httpPost);
			StatusLine statusLine = response.getStatusLine();

			if (Debug.DEBUG) {
				Log.d(TAG, "status: " + statusLine);
			}

			return (statusLine.getStatusCode() == HttpStatus.SC_OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
